package ua.mihaylenko.cardsplugin.utils
{
import flash.utils.ByteArray;
import flash.net.URLVariables;

public class StringUtil
{
	//--------------------------------------------------------------------------
	//
	//  Class methods
	//
	//--------------------------------------------------------------------------

	public static function trim(input:String):String
	{
		return StringUtil.ltrim(StringUtil.rtrim(input));
	}

	public static function ltrim(input:String):String
	{
		var size:Number = input.length;
		for(var i:Number = 0; i < size; i++)
		{
			if(input.charCodeAt(i) > 32)
			{
				return input.substring(i);
			}
		}
		return "";
	}

	public static function rtrim(input:String):String
	{
		var size:Number = input.length;
		for(var i:Number = size; i > 0; i--)
		{
			if(input.charCodeAt(i - 1) > 32)
			{
				return input.substring(0, i);
			}
		}
		return "";
	}

    public static function substitute(input:String, ... rest):String
    {
        // Replace all of the parameters in the msg string.
        var len:uint = rest.length;
        var args:Array;
        if (len == 1 && rest[0] is Array)
        {
        	args = rest[0] as Array;
        	len = args.length;
        } else {
            args = rest;
        }
		for (var i:int = 0; i < len; i++) {
            input = input.replace(new RegExp("\\{"+i+"\\}", "g"), args[i]);
		}
        return input;
    }

    public static function substituteByID(input:String, containObj:Object):String
    {
    	// Replace all of the paramenters in the passed string.
    	for ( var i:String in containObj) {
    		input = input.replace(new RegExp("\\{\\$"+i+"\\}", "g"), containObj[i]);
    	}
    	return input;
    }

	public static function replace(input:String, replace:String, replaceWith:String):String
	{
		return input.split(replace).join(replaceWith);
	}

	/**
	 * intercept and return part of the specified String
	 * @param input
	 * @param limited
	 * @param addedChars
	 * @return
	 */
	public static function intercept(input:String,limited:uint,addedChars:String):String
	{
		if(input == null) return "";
		if(input.length > limited && limited > 0) {
			input = input.slice(0,limited);
			input += addedChars;
		}
		return input;
	}

	/**
	 * HTML encode
	 */
	public static function htmlEncode(input:String):String
	{
		input = replace(input,"&","&amp;");
		input = replace(input,"\"","&quot;");
		input = replace(input,"'","&apos;");
		input = replace(input,"<","&lt;");
		input = replace(input,">","&gt;");
		return input;
	}

	public static function htmlDecode(string:String):String {
		string = replace(string,"&amp;","&");
		string = replace(string,"&quot;","\"");
		string = replace(string,"&apos;","'");
		string = replace(string,"&lt;","<");
		string = replace(string,"&gt;",">");
		return string;
	}
	/**
	 * Make a string CDATA
	 */
	public static function cdata(theURL:String):XML
	{
	    var x:XML = new XML("<![CDATA[" + theURL + "]]>");
	    return x;
	}

	/**
	 * Strip out <, >
	 */
	 public static function stripGtAndLt(input:String):String
	 {
		input = replace(input,"<","&lt;");
		input = replace(input,">","&gt;");
		return input;
	 }

	/**
     * validate Email address
     */
	public static function validateEmail(input:String):Boolean
	{
        var pattern:RegExp = /(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}+/;
        var result:Object = pattern.exec(input);
        if(result == null) {
            return false;
        }
        return true;
    }

    /**
    * Encoding for String
    * Change GB2312 to UTF8
    */
	public static function GB2312toUTF8(input:*):String
	{
		var ba:ByteArray = ByteArray(input);
		return ba.readMultiByte(ba.length, "gb2312");
	}

	/**
	 * add zero padding
	 */
	public static function zeroPad(num:uint):String
	{
		var output:String;
		if (num < 10)
		{
			output = "0" + num;
		}
		else
		{
			output = String(num);
		}
		return output;
	}
	
	/**
     * url encode
     **/
    public static function URLEncode(instr:String):String
    {
        var tmpv:URLVariables = new URLVariables();
            tmpv.v            = instr;
         
        var restr:String      = tmpv.toString();
    
        return restr.substr(2,restr.length);
    }
}
}